package com.gitee.neuray.security.admin.role.controller;

import com.alibaba.fastjson.JSON;
import com.gitee.neuray.security.admin.authority.AdminMenuDto;
import com.gitee.neuray.security.admin.role.entity.AdminRoleEntity;
import com.gitee.neuray.security.admin.role.biz.AdminRoleBiz;
import com.gitee.neuray.security.admin.vo.AdminPermission;
import com.gitee.neuray.security.common.msg.ListRestResponse;
import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import org.springframework.web.bind.annotation.*;
import com.gitee.neuray.security.common.rest.BaseController;

import java.util.List;
import java.util.Map;


/**
 * 代码生成
 *
 * @author zhoyou
 * @email ${email}
 * @date 2020-03-10 16:06:52
 */
@RestController
@RequestMapping("pmRole")
public class AdminRoleController extends BaseController<AdminRoleBiz, AdminRoleEntity>{
    /**
     * 根据角色id获取角色菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/permission", method = RequestMethod.GET)
    @ResponseBody
    public ListRestResponse<List<AdminMenuDto>> getMenuAuthority(@RequestParam("id") String id) {
        List<AdminMenuDto> adminMenuDtos = baseBiz.selectAuthorityMenuTree(id);
        return new ListRestResponse<>("",adminMenuDtos.size(),adminMenuDtos);
    }
    /**
     * 批量修改角色菜单
     * @param permissionMap  包含 角色id和权限list
     */
    @RequestMapping(value = "/permission", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse modifyMenuAuthority(@RequestBody Map<String,Object> permissionMap) {
        String id =(String)permissionMap.get("id");
        List<AdminMenuDto> permissionList;
        JSON.toJSONString(permissionMap.get("permissionList"));
        permissionList = JSON.parseArray(JSON.toJSONString(permissionMap.get("permissionList")),AdminMenuDto.class);
        baseBiz.modifyAuthorityMenus(id,permissionList);
        return new ObjectRestResponse().rel(true);
    }
}
